package org.opens.controller;

import org.apache.catalina.core.ApplicationContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RequestMapping(value = "/api/nacos/config")
@RestController
@RefreshScope
public class NacosConfigController {

    @Autowired
    private ConfigurableApplicationContext applicationContext;

    @Value("${config.app-name}")
    private String appName;

    @Value("${config.env}")
    private String env;

    @GetMapping(value = "/app-name")
    public String getProperties() {
        return applicationContext.getEnvironment().getProperty("config.app-name");
    }

    /**
     * 说明：
     * <pre>
     *     1. 单使用@Value这种方式注入配置属性是无法动态刷新的，必须在类上添加@RefreshScope注解才能使属性动态加载。
     * </pre>
     */
    @GetMapping(value = "/app-name2")
    public String getProperties2() {
        return appName;
    }

    @GetMapping(value = "/env")
    public String getProperties3() {
        return env;
    }

}
